#include<sys/socket.h>
#include<arpa/inet.h>
#include<string.h>

#include"mj.h"

int sock,r;
char gbuffer[OUTPUT_SIZE];
struct sockaddr_in addr;
socklen_t glen;

void 
printHuResult(char *buf) {
    vector<string> lv {};
    vector<string> wv {};
    supersplit(string(buf), lv, "-");
    for(auto l : lv) {
        wv.clear();
        supersplit(l, wv, ".");
        if(wv[0]=="request_id") {
            cout << wv[3] << "张牌打掉后可听" << endl;
        } else if (wv[0] == "out_tile") {
            cout << "out:" << wv[1] << endl;
        } else {
            cout << "get:" << wv[0] << " " << wv[1] << "番 ";
            for(unsigned int i=0; i<wv.size()-2; i++) {
                cout << fanPattern.at((MJ_FANXING)stol(wv[i+2])).cn_name << " ";
            }
            cout << endl;
        }
    }
    cout << endl;
}

void 
loopInputFile(std::istream &fin, bool is_raw_input) {
    //std::ifstream fin("../hu_dict/online.txt");
    std::string l;

    while(getline(fin, l)) {
        const char *input;
        int input_len;
        if(is_raw_input) {
            vector<string> v {};
            supersplit(l, v, ".");
            if(v.size()==52)
                l.append(".4.0");
            input = l.c_str();
            input_len = l.size();
        } else {
            std::vector<uint8_t> lts;
            for(unsigned i=0; i<l.size(); i++) {
                char c = l.c_str()[i];
                if(c<='I' and c>='A') lts.push_back(c - 'A' + 11);
                if(c<='i' and c>='a') lts.push_back(c - 'a' + 1);
            }
            // write the request
            snprintf(gbuffer, INPUT_SIZE, 
                     "1.%d.13.%d.%d.%d.%d.%d.%d.%d.%d.%d.%d.%d.%d.%d.0.35.2.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.4.0", 
                     lts[0], lts[1], lts[2], lts[3], lts[4], lts[5], lts[6],
                     lts[7], lts[8], lts[9], lts[10], lts[11], lts[12], lts[13]);
            input = gbuffer;
            input_len = strlen(gbuffer);
        }

        r = sendto(sock, input, input_len, 0, (struct sockaddr*)&addr, glen);
        if(r != (int)input_len) {
            printf("sendto error\n");
            return;
        }
        printf("write len=%d\n%s\n",r, input);  

        r = recvfrom(sock, gbuffer, sizeof(gbuffer), 0, NULL, NULL);
        if(r <0) {
            printf("recv error\n");
            return;
        }	   
        gbuffer[r]='\0';
        printf("recvfrom len=%d rawbuf=\n%s\nhumanbuf=\n",r, gbuffer);
        if(strncmp(gbuffer, "request_id", 10)==0)
            printHuResult(gbuffer);
    }
}

int 
main(int argc,char *argv[]) {
	if(argc!=5) {
		printf("mjcli <ip> <port> <filepath|stdin> <israw>\n");
		return 1;
	}

	memset(&addr,0,sizeof(addr));
	sock=socket(AF_INET,SOCK_DGRAM,0);
	addr.sin_family=AF_INET;
	addr.sin_addr.s_addr=inet_addr(argv[1]);
	addr.sin_port=htons(atoi(argv[2]));
	glen=sizeof(addr);

    if(strncmp(argv[3], "stdin", 5)==0) 
        loopInputFile(std::cin, true);
    else {
        std::ifstream fin(argv[3]);
        loopInputFile(fin, atoi(argv[4]));
    }

    close(sock);
    return 0;
}
